<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    console.time();
    // for 循环    用var 性能一样  let 性能好  原因块级比全局能性能消耗小
    // while 循环  用var 性能一样  let 性能差
    Array.prototype.myForEach = function (fn, thisValue) {
      if (typeof fn !== 'function') {
        throw new Error(`${fn} 不是一个函数`)
      }
      if ([null, undefined].includes(this)) {
        throw new Error(`this 是null 或者 undefined`)
      }
      const arr = Object(this)
      for (let i = 0; i < arr.length; i++) {
        fn.call(thisValue, arr[i], i, arr)
      }
    }
    let arr = [1, 2, 3, 4]
    arr.myForEach(item => {
      console.log(item)
    })
  </script>
</body>

</html>